//
//  LYArtViewController.m
//  artist
//
//  Created by Jay_Apple on 16/3/3.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYArtViewController.h"
#import "LYEArtistCellHeaderModel.h"
#import "LYEArtisitCellHeaderView.h"
#import "LYArtHeaderViewController.h"
#import "LYCircleListModel.h"
#import "LYCommentListViewController.h"
#import "LYPostDetailModel.h"
#import "LYFriendCell.h"
#import "LYStuWebViewController.h"

@interface LYArtViewController ()<UITableViewDataSource, VVYRefreshTableViewDelegate, LYFriendCellDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSMutableArray *topics;

@property (assign, nonatomic) NSInteger page;

@end

@implementation LYArtViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化tableview
    [self setUpTableView];
    
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self.tableView selector:@selector(reloadData) name:LYImageDownloadFinished object:nil];
    
    [self setUpThemeNotification];
    
    
    //获取帖子
    [self.tableView.mj_header beginRefreshing];
    
    
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self.tableView];
    [self resignThemeNotification];
}

- (void)upDateTheme{
    
    [self loadNewData];
}




#pragma mark - 重写
- (void)setModel:(LYCircleListModel *)model{
    
    _model = model;
    
    self.title = _model.name;
    
    
}

#pragma mark - 上下拉刷新
/**
 *  下拉
 */
- (void)tableViewHeaderRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadNewData];
    
}

/**
 *  上拉
 */
- (void)tableViewfooterRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadMoreData];
    
}

#pragma mark - 网络请求

/**
 *  获取帖子
 */
- (void)loadNewData{
    
    [LYRequestTool acquirPostCircleListWithToken:LYToken type:@"circle" circle_id:self.model.circle_id page:@"1" per_page:@"10" success:^(NSArray<LYPostDetailModel *> *models) {
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer resetNoMoreData];
        self.topics = [models mutableCopy];
        [self.tableView reloadData];
        self.page = 1;
        
        
    } failure:^(NSString *code, NSString *message) {
        [self.tableView.mj_header endRefreshing];
    }];
}

/**
 *  加载更多数据
 */
- (void)loadMoreData{
    
    NSString *page = [NSString stringWithFormat:@"%zd",self.page + 1];
    
    
    [LYRequestTool acquirPostCircleListWithToken:LYToken type:@"circle" circle_id:self.model.circle_id page:page per_page:@"10" success:^(NSArray<LYPostDetailModel *> *models) {
        
        
        if (models.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        
        [self.tableView.mj_footer endRefreshing];
        
        [self.topics addObjectsFromArray:models];;
        self.page++;
        [self.tableView reloadData];
        
        
        
        
    } failure:^(NSString *code, NSString *message) {
        [self.tableView.mj_footer endRefreshing];
    }];
    
}


/**
 *  点赞
 */
- (void)praiseWithTopicID:(NSInteger)topicID{
    
    [LYRequestTool praiseWithToken:LYToken type:ObjectTypePost data_id:[NSString stringWithFormat:@"%zd",topicID] success:nil failure:nil];
}


/**
 *  取消点赞
 */
- (void)cancelPraiseWithTopicID:(NSInteger)topicID{
    
    [LYRequestTool cancelPraiseWithToken:LYToken type:ObjectTypePost data_id:[NSString stringWithFormat:@"%zd",topicID] success:nil failure:nil];
    
}

/**
 *  收藏
 */
- (void)collectWithTopicID:(NSInteger)topicID{
    
    [LYRequestTool collectionWithToken:LYToken type:ObjectTypePost data_id:[NSString stringWithFormat:@"%zd",topicID] success:nil failure:nil];
}


/**
 *  取消收藏
 */
- (void)cancelCollectWithTopicID:(NSInteger)topicID{
    
    
    [LYRequestTool cancelCollectionWithToken:LYToken type:ObjectTypePost data_id:[NSString stringWithFormat:@"%zd",topicID] success:nil failure:nil];
    
}

#pragma mark - 初始化
/**
 *  初始化tableview
 */
- (void)setUpTableView{
    
    //header
    LYArtHeaderViewController *artheaderVC = [[LYArtHeaderViewController alloc] init];
    artheaderVC.model = self.model;
    [self addChildViewController:artheaderVC];
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LYScreenWidth, LYArtHeaderHeight)];
    artheaderVC.view.My_Size = CGSizeMake(header.My_Width, header.My_Height);
    [header addSubview:artheaderVC.view];
    self.tableView.tableHeaderView = header;
    
    
}


#pragma mark - tableViewDelegate

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    LYFriendCell *friendCell = (LYFriendCell *)cell;
    
    LYPostDetailModel *model = self.topics[indexPath.row];
    friendCell.model = model;
    friendCell.delegate = self;
    
}

#pragma mark - tableViewDataSource
/**
 *  cell
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LYFriendCell *cell = [LYFriendCell cellWithTableView:tableView andIndetifier:LYFriendCellIdentifier];
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.topics.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LYPostDetailModel *model = self.topics[indexPath.row];
    return model.rowHeight;
}

#pragma mark - friendCellDelegate
- (void)cellIconOnclick:(LYFriendCell *)cell{
    
  
        LYStuWebViewController *stuWebVC = [LYStuWebViewController stuWebVCWithID:cell.model.user_id];
        [self.navigationController pushViewController:stuWebVC animated:YES];
    
}

- (void)cellShowContentBtnOnclick:(LYFriendCell *)cell{
    
    cell.model.showContent = !cell.model.showContent;
    [self.tableView reloadData];
}

- (void)cell:(LYFriendCell *)cell bottomBarBtnOnclickWithType:(cellBtnType)type{
    
    [[VVYCheckStatus sharedInstance] checkIsLoginBlock:^(LYUserInfoModel *userInfo) {
        
        LYPostDetailModel *model = cell.model;
        
        switch (type) {
            case cellBtnTypeParise:
                
                model.is_praise = !model.is_praise;
                if (model.is_praise) {
                    model.praise_num++;
                    
                    [self praiseWithTopicID:model.topic_id];
                    
                }else{
                    model.praise_num--;
                    [self cancelPraiseWithTopicID:model.topic_id];
                }
                
                
                
                break;
                
            case cellBtnTypeCollect:
                
                model.is_collect = !model.is_collect;
                if (model.is_collect) {
                    model.collect_num++;
                    [self collectWithTopicID:model.topic_id];
                    
                }else{
                    model.collect_num--;
                    [self cancelCollectWithTopicID:model.topic_id];
                }
                
                break;
                
            case cellBtnTypeComment:{
                
                LYCommentListViewController *commentVC = [[LYCommentListViewController alloc] init];
                commentVC.data_id = [@(model.topic_id) stringValue];
                commentVC.type = CommentTypeTopic;
                [self.navigationController pushViewController:commentVC animated:YES];
            }
                break;
                
        }
        
        [self.tableView reloadData];
        
        
    }];
    
    
}


@end
